<template>
  <div class="login-container">
    <van-nav-bar class="page-nav-bar" title="登录">
      <van-icon slot="left" name="cross" @click="$router.back()" />
    </van-nav-bar>

    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down v-if="isCountDownShow" :time="60 * 1000" format="ss s" @finish="isCountDownShow = false" />
          <van-button v-else native-type="button" @click="onSendSms" class="send-sms-btn" round size="small">发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user'

export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '18811655888',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3|5|6|8]\d{9}$/, message: '请输入正确格式手机号' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' }
        ]
      },
      isCountDownShow: false
    }
  },
  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        duration: 0
      })

      const user = this.user
      try {
        const res = await login(user)
        this.$toast.success({
          message: '登录成功...'
        })

        this.$store.commit('setUser', res.data.data)
        this.$router.back()

      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail({
            message: '登录失败...'
          })
        }
      }
    },
    async onSendSms () {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        console.log(11111, err)
        return false
      }

      this.isCountDownShow = true

      try {
        await getSmsCode(this.user.mobile)
        this.$toast.success({
          message: '已送验证码'
        })
      } catch (err) {
        const res = err.response
        if (res.status === 429 ) {
          this.$toast.fail({
            message: res.data.message
          })
        } else {
          this.$toast.fail({
            message: '发送验证码失败'
          })
        }
      }

    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    padding: 0;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    vertical-align: top;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>

<style lang="less">
.login-container {
  .van-field__button {
    height: 46px;
  }
}
</style>
